export default function ({
  isHMR,
  app,
  store,
  route,
  params,
  error,
  redirect,
  req,
}) {
  if (isHMR || route.fullPath.startsWith(`/search?q=`)) {
    return
  }
  //谷歌爬虫快照不进行拦截
  const defaultLocale = app.i18n.fallbackLocale
  let locale

  if (
    ((route.name === 'lang-tool-handle' || route.name === 'tool-handle') &&
      !route.params.handle) ||
    !route.name
  ) {
    return error({
      message: 'This page could not be found.',
      statusCode: 404,
    })
  }

  /* if (
    route.fullPath === '/' ||
    route.fullPath === '/en' ||
    route.fullPath === '/en/'
  ) {
    //浏览器的语言
    let navigatorLangStr = process.server
      ? req.headers['accept-language']
      : navigator.language
    let navigatorLangArr = []

    if (navigatorLangStr && typeof navigatorLangStr === 'string') {
      navigatorLangArr = navigatorLangStr.split(',')
    } else {
      navigatorLangArr[0] = 'en'
    }
    let navigatorLang = navigatorLangArr[0]

    if (navigatorLang === 'zh-CN') {
      navigatorLang = 'zh'
    } else if (navigatorLang === 'zh-TW') {
      navigatorLang = 'tw'
    } else if (navigatorLang === 'en-US') {
      navigatorLang = 'en'
    }

    if (!store.state.locales.includes(navigatorLang)) {
      navigatorLang = 'en'
    }

    locale =
      params.lang ||
      app.$cookies.get('locale') ||
      navigatorLang ||
      defaultLocale
  } else {
    locale = params.lang || app.$cookies.get('locale') || defaultLocale
  } */

  locale = params.lang || app.$cookies.get('locale') || defaultLocale
  if (!store.state.locales.includes(locale)) {
    return error({
      message: 'This page could not be found.',
      statusCode: 404,
    })
  }

  store.commit('SET_LANG', locale)
  app.i18n.locale = store.state.locale

  let toolify_isLogin = app.$cookies.get('toolify_isLogin')
    ? app.$cookies.get('toolify_isLogin')
    : false
  store.commit('SET_ISLOGIN', toolify_isLogin)

  if (
    //promote 重定向
    route.name === 'lang-promote' ||
    route.name === 'promote'
  ) {
    return redirect(locale === 'en' ? `/advertise` : `/${locale}/advertise`)
  } else if (
    //分类属性 重定向
    (route.name === 'lang-category-attribute-handle' ||
      route.name === 'category-attribute-handle') &&
    !params.handle
  ) {
    return redirect(locale === 'en' ? `/category` : `/${locale}/category`)
  } else if (
    //更新工具 重定向
    (route.name === 'lang-tool-update-handle' ||
      route.name === 'tool-update-handle') &&
    !params.handle
  ) {
    return redirect(locale === 'en' ? `/update-ai` : `/${locale}/update-ai`)
  } else if (
    //登录拦截
    toolify_isLogin &&
    (route.name === 'lang-login' ||
      route.name === 'lang-register' ||
      route.name === 'login' ||
      route.name === 'register')
  ) {
    return redirect(locale === 'en' ? '/' : `/${locale}/`)
  } else if (
    //未登录拦截
    !toolify_isLogin &&
    (route.name === 'lang-favorites' ||
      route.name === 'favorites' ||
      route.name === 'user' ||
      route.name === 'user')
  ) {
    return redirect(
      locale === 'en'
        ? `/login?from=${route.name}`
        : `/${locale}/login?from=${route.name}`
    )
  } else if (
    locale === defaultLocale &&
    route.fullPath.indexOf('/' + defaultLocale) === 0
  ) {
    //英语隐藏
    const toReplace =
      '^/' +
      defaultLocale +
      (route.fullPath.indexOf('/' + defaultLocale + '/') === 0 ? '/' : '')
    const re = new RegExp(toReplace)
    return redirect(route.fullPath.replace(re, '/'))
  } else if (!params.lang && locale && locale !== 'en' && route.name) {
    //非英文环境下链接没有语言
    return redirect(`/${locale}${route.fullPath}`)
  }
  /* else if (params.lang && locale !== params.lang) {
    return redirect(route.fullPath.replace(`/${params.lang}`, `/${locale}`))
  } */
}
